package com.eascs.fp.uc.untils;

import java.util.regex.Pattern;

import org.apache.commons.lang3.StringUtils;

public final class IDCardUtils {
	/**
     * 验证身份证号格式是否正确
     *
     * @param idcard
     * @return
     */
    public static boolean validateIDCard(String idcard) {
        boolean IS_IDCARD = false;
        if (StringUtils.isBlank(idcard) || (idcard.length() != 15 && idcard.length() != 18)) {
            return IS_IDCARD;
        }
        String regex = "";
        // 判断是否是15位身份证号
        if (idcard.length() == 15) {
            regex = "^[1-9]\\d{7}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}$";
            IS_IDCARD = regexMatch(regex, idcard);
        } else if (idcard.length() == 18) {
            regex = "^[1-9]\\d{5}[1-9]\\d{3}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}([\\d|x|X]{1})$";
            IS_IDCARD = regexMatch(regex, idcard);
        }
        if (!IS_IDCARD)
            return IS_IDCARD;

        IS_IDCARD = false;
        int[] monthPerDays = new int[] { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
        String certid = idcard.toLowerCase();
        String birthDate = certid.length() == 15 ? "19" + certid.substring(6, 12) : certid.substring(6, 14);
        String year = birthDate.substring(0, 4);
        String month = null;
        String day = null;
        if (birthDate.substring(4, 5).equals("0"))
            month = birthDate.substring(5, 6);
        else
            month = birthDate.substring(4, 6);
        if (birthDate.substring(6, 7).equals("0"))
            day = birthDate.substring(7, 8);
        else
            day = birthDate.substring(6, 8);
        int dd = Integer.parseInt(day);
        int mm = Integer.parseInt(month);
        int yy = Integer.parseInt(year);
        if (mm > 12 || mm < 1 || dd > 31 || dd < 1)
            return IS_IDCARD;

        if (yy % 100 != 0) {
            if (yy % 4 == 0)
                monthPerDays[1] = 29;
        } else {
            if (yy % 400 == 0)
                monthPerDays[1] = 29;
        }
        if (monthPerDays[mm - 1] < dd)
            return IS_IDCARD;

        if (certid.length() == 15)
            return true;

        int[] arTemp = new int[] { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 };
        int num = 0;
        String proof = null;
        for (int i = 0; i < 17; i++) {
            num = num + Integer.parseInt(certid.substring(i, i + 1)) * arTemp[i];
        }
        num = num % 11;
        switch (num) {
        case 0:
            proof = "1";
            break;
        case 1:
            proof = "0";
            break;
        case 2:
            proof = "x";
            break;
        case 3:
            proof = "9";
            break;
        case 4:
            proof = "8";
            break;
        case 5:
            proof = "7";
            break;
        case 6:
            proof = "6";
            break;
        case 7:
            proof = "5";
            break;
        case 8:
            proof = "4";
            break;
        case 9:
            proof = "3";
            break;
        case 10:
            proof = "2";
            break;
        }
        if (StringUtils.isBlank(proof) || !certid.substring(17, 18).equals(proof)) {
            return IS_IDCARD;
        } else {
            return true;
        }
    }
    
    /**
     * 身份证信息屏蔽
     * @param idcard
     * @return
     */
    public static String closedIDCard(String idcard) {
    	if(StringUtils.isBlank(idcard) || idcard.length() < 8){
    		return idcard;
    	}
    	return idcard.substring(0,3) + idcard.substring(3,idcard.length()).replaceAll(".", "*");
    }
    
    /**
     * 验证正则表达式是否合法
     *
     * @param regex
     * @param str
     * @return
     */
    private static boolean regexMatch(String regex, String str) {
        Pattern pattern = Pattern.compile(regex);
        return pattern.matcher(StringUtils.trimToEmpty(str)).matches();
    }
}
